import dayjs from 'dayjs';

const time = new Date();
time.setHours(0, 0, 0, 0);

export function formatMillisecond(millisecond: number, formatter = 'HH:mm:ss SSS'): string {
  return dayjs(time).millisecond(millisecond).format(formatter);
}

export function formatDuration(millisecond: number): string {
  const duration = dayjs(time).millisecond(millisecond);
  const hours = duration.hour();
  const minutes = duration.minute();
  const seconds = duration.second();
  const milliseconds = duration.millisecond();

  let res = '';
  if (hours > 0) {
    res += `${hours}小时`;
  }

  if (minutes > 0) {
    res += `${minutes}分${hours <= 0 && seconds <= 0 && milliseconds <= 0 ? '钟' : ''}`;
  }

  if (seconds > 0) {
    res += `${seconds}秒`;
  }

  if (milliseconds > 0) {
    res += `${milliseconds}毫秒`;
  }

  return res;
}